﻿using HKB.Common;
using HKB.Web;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControls_HowaveProvinceCity : UserControl
{
    private Conn conn = null;
    private Shop_City _city;
    private string m_selectedProv = "";
    private string m_selectedCity;
    private string m_selectedArea = "";
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected Shop_City m_city
    {
        get
        {
            Shop_City city;
            if (this._city == null)
            {
                this._city = new Shop_City();
                city = this._city;
            }
            else
            {
                city = this._city;
            }
            return city;
        }
    }
    public string SelectedProvince
    {
        get
        {
            if (this.Province.Items.Count > 0)
            {
                this.m_selectedProv = this.Province.SelectedValue;
            }
            return this.m_selectedProv;
        }
        set
        {
            this.m_selectedProv = value;
        }
    }
    public string SelectedCity
    {
        get
        {
            return this.City.SelectedValue;
        }
        set
        {
            this.m_selectedCity = value;
        }
    }
    public string SelectedText
    {
        get
        {
            return this.Province.SelectedValue + this.City.SelectedValue;
        }
    }
    public string SelectedArea
    {
        get
        {
            return this.ddlArea.SelectedValue;
        }
        set
        {
            this.m_selectedArea = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {
            this.Bind();
        }
        base.Error += new EventHandler(this.Shop_UserControls_HowaveProvinceCity_Error);
    }
    private void Shop_UserControls_HowaveProvinceCity_Error(object sender, EventArgs e)
    {
        if (this.conn != null)
        {
            this.conn.CloseConn();
        }
    }
    protected void Province_Changed(object seder, EventArgs e)
    {
        this.GetCity();
        this.GetArea();
    }
    public void Bind()
    {
        this.BindProvince();
        this.BindCity();
        this.BindArea();
    }
    protected void BindProvince()
    {
        DataTable allProvince = this.m_city.GetAllProvince();
        this.Province.Items.Clear();
        ListItem item = new ListItem(Language.Get("Common/Choose"), "");
        this.Province.Items.Add(item);
        this.Province.DataSource = allProvince;
        this.Province.DataTextField = "Province";
        this.Province.DataValueField = "Province";
        this.Province.DataBind();
        if (this.m_selectedProv != null && this.m_selectedProv != "")
        {
            foreach (ListItem listItem in this.Province.Items)
            {
                if (listItem.Value == this.m_selectedProv)
                {
                    listItem.Selected = true;
                    break;
                }
            }
        }
    }
    protected void BindCity()
    {
        if (this.Province.SelectedIndex != 0)
        {
            this.GetCity();
            foreach (ListItem listItem in this.City.Items)
            {
                if (listItem.Value == this.m_selectedCity)
                {
                    listItem.Selected = true;
                    break;
                }
            }
        }
        else
        {
            this.City.Items.Clear();
            this.City.Items.Add(new ListItem(Language.Get("Common/Choose"), ""));
        }
    }
    protected void GetCity()
    {
        this.City.Items.Clear();
        this.City.Items.Add(new ListItem(Language.Get("Common/Choose"), ""));
        if (this.Province.SelectedIndex != 0)
        {
            DataTable city = this.m_city.GetCity(this.Province.SelectedValue);
            if (city != null && city.Rows.Count > 0)
            {
                this.City.DataSource = city;
                this.City.DataTextField = "city";
                this.City.DataValueField = "city";
                this.City.DataBind();
            }
        }
    }
    protected void GetArea()
    {
        this.ddlArea.Items.Clear();
        this.ddlArea.Items.Add(new ListItem(Language.Get("Common/Choose"), ""));
        if (this.City.SelectedIndex != 0)
        {
            DataTable allAreaByCity = this.m_city.GetAllAreaByCity(this.City.SelectedValue);
            if (allAreaByCity != null && allAreaByCity.Rows.Count > 0)
            {
                this.ddlArea.DataSource = allAreaByCity;
                this.ddlArea.DataTextField = "area";
                this.ddlArea.DataValueField = "area";
                this.ddlArea.DataBind();
            }
        }
    }
    protected void BindArea()
    {
        if (this.City.SelectedIndex != 0)
        {
            this.GetArea();
            foreach (ListItem listItem in this.ddlArea.Items)
            {
                if (listItem.Value.Equals(this.m_selectedArea))
                {
                    listItem.Selected = true;
                    break;
                }
            }
        }
        else
        {
            this.ddlArea.Items.Clear();
            this.ddlArea.Items.Add(new ListItem(Language.Get("Common/Choose"), ""));
        }
    }
    protected void City_Changed(object seder, EventArgs e)
    {
        this.GetArea();
    }
}
